package com.javasm.service.impl;

import com.alibaba.fastjson.JSON;
import com.javasm.dao.AdminMenuDao;
import com.javasm.entity.AdminMenu;
import com.javasm.service.MenuService;
import com.javasm.utils.MyBatisHeapler;
import org.apache.ibatis.session.SqlSession;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @version 0.1
 * @className: MenuServiceImpl
 * @description:
 * @author: gfs
 * @date: 2025/6/20 11:07
 * @since: jdk17
 */
public class MenuServiceImpl implements MenuService {

    @Override
    public List<AdminMenu> listMenuByCondition(AdminMenu inputMenu, Integer page, Integer pageSize) {
        //table使用的数据
        SqlSession sqlSession = MyBatisHeapler.getSqlSession();
        AdminMenuDao mapper = sqlSession.getMapper(AdminMenuDao.class);
        List<AdminMenu> adminMenus = mapper.listMenuByCondition(inputMenu,(page-1)*pageSize,pageSize);
        MyBatisHeapler.backAndSaveSqlSession(sqlSession);
        return adminMenus;
    }

    @Override
    public Integer countMenuByCondition(AdminMenu inputMenu) {
        SqlSession sqlSession = MyBatisHeapler.getSqlSession();
        AdminMenuDao mapper = sqlSession.getMapper(AdminMenuDao.class);

        Integer countNum = mapper.countMenuByCondition(inputMenu);
        MyBatisHeapler.backAndSaveSqlSession(sqlSession);
        return countNum;
    }

    @Override
    public List<AdminMenu> listMenuLevel1() {
        SqlSession sqlSession = MyBatisHeapler.getSqlSession();
        AdminMenuDao mapper = sqlSession.getMapper(AdminMenuDao.class);

        List<AdminMenu> adminMenusLevel1 = mapper.listMenuLevel1();
        MyBatisHeapler.backAndSaveSqlSession(sqlSession);
        return adminMenusLevel1;
    }


    @Override
    public List<String> listMenuIcon() {
        SqlSession sqlSession = MyBatisHeapler.getSqlSession();
        AdminMenuDao mapper = sqlSession.getMapper(AdminMenuDao.class);
        List<String> listIcon = mapper.listMenuIcon();
        System.out.println(listIcon);
        MyBatisHeapler.backAndSaveSqlSession(sqlSession);
        return listIcon;
    }

    @Override
    public Integer insertMenu(AdminMenu inputMenu) {
        SqlSession sqlSession = MyBatisHeapler.getSqlSession();
        AdminMenuDao mapper = sqlSession.getMapper(AdminMenuDao.class);
        Integer resNum = mapper.insertMenu(inputMenu);
        MyBatisHeapler.backAndSaveSqlSession(sqlSession);
        return resNum;
    }


    @Override
    public AdminMenu getById(Long mid) {
        SqlSession sqlSession = MyBatisHeapler.getSqlSession();
        AdminMenuDao mapper = sqlSession.getMapper(AdminMenuDao.class);
        AdminMenu menuById = mapper.getById(mid);
        MyBatisHeapler.backAndSaveSqlSession(sqlSession);
        return menuById;
    }

    @Override
    public Integer updateMenu(AdminMenu inputMenu) {
        SqlSession sqlSession = MyBatisHeapler.getSqlSession();
        AdminMenuDao mapper = sqlSession.getMapper(AdminMenuDao.class);
        Integer resNum = mapper.updateMenu(inputMenu);
        MyBatisHeapler.backAndSaveSqlSession(sqlSession);
        return resNum;
    }

    @Override
    public Integer deleteMenu(Long mid) {
        SqlSession sqlSession = MyBatisHeapler.getSqlSession();
        AdminMenuDao mapper = sqlSession.getMapper(AdminMenuDao.class);
        Integer resNum = mapper.deleteMenu(mid);
        MyBatisHeapler.backAndSaveSqlSession(sqlSession);
        return  resNum;
    }

    @Override
    public Integer deleteMenuMultiple(List<Long> listMid) {
        SqlSession sqlSession = MyBatisHeapler.getSqlSession();
        AdminMenuDao mapper = sqlSession.getMapper(AdminMenuDao.class);
        Integer resNum = mapper.deleteMenuMultiple(listMid);
        MyBatisHeapler.backAndSaveSqlSession(sqlSession);
        return  resNum;
    }

    @Override
    public List<AdminMenu> ListAllMenuWithSubMenu() {
        SqlSession sqlSession = MyBatisHeapler.getSqlSession();
        AdminMenuDao mapper = sqlSession.getMapper(AdminMenuDao.class);

        List<AdminMenu> adminMenus = mapper.ListAllMenuWithSubMenu();

        MyBatisHeapler.backSqlSession(sqlSession);
        return adminMenus;
    }

    @Override
    public List<Long> ListUserMenuId(Long uid) {
        SqlSession sqlSession = MyBatisHeapler.getSqlSession();
        AdminMenuDao mapper = sqlSession.getMapper(AdminMenuDao.class);

        List<Long> longs = mapper.ListUserMenuId(uid);
        System.out.println(longs);
        MyBatisHeapler.backSqlSession(sqlSession);
        return longs;
    }
}
